import Vue from "vue";
import VueRouter from "vue-router";
import Home from "../views/Home.vue"
import Logoin from "../views/Logoin.vue";
Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    name: "Logoin",
    component: Logoin
  },
  {
    path: "/home",
    name: "Home",
    redirect:"/home/homee",
    component: Home
  },
  {
    path: '/home',
    name: 'Home1',
    component: Home,
    children: [
      {
        path: 'Userlist',
        component: () =>
          import("../components/Userlist")
      },
      {
        path:"Homee",
        component:() => 
        import("../components/Homee")
      }
    ]
  },
];

const router = new VueRouter({
  routes,
});
router.beforeEach((to,from,next)=>{ // 路由守卫不使用循环
  // to去哪里 form上一个
  let token = localStorage.getItem("token")
   console.log(token)
  console.log(to,from)
  if(to.path === "/Logoin"){
    next()
  }else{
    if(token){
      next()
    }else{
      next({name:"Logoin"})
    }
  }
  next()
})

export default router;
